/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.example.model;

import com.wisnu.example.database.Database;
import com.wisnu.example.entity.Ptik;
import com.wisnu.example.error.PtikException;
import com.wisnu.example.model.event.PtikListener;
import com.wisnu.example.service.PtikDao;
import java.sql.SQLException;
import java.util.Date;

/**
 *
 * @author Wisnu Wardoyo
 */
public class PtikModel {

    Integer id;
    String nim;
    String nama;
    Date tanggalLahir;
    String telepon;
    String email;

    PtikListener listener;

    public void setListener(PtikListener listener) {
        this.listener = listener;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        fireOnChage();
    }

    public String getNim() {
        return nim;
    }

    public void setNim(String nim) {
        this.nim = nim;
        fireOnChage();
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
        fireOnChage();
    }

    public Date getTanggalLahir() {
        return tanggalLahir;
    }

    public void setTanggalLahir(Date tanggalLahir) {
        this.tanggalLahir = tanggalLahir;
        fireOnChage();
    }

    public String getTelepon() {
        return telepon;
    }

    public void setTelepon(String telepon) {
        this.telepon = telepon;
        fireOnChage();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
        fireOnChage();
    }

    protected void fireOnChage() {
        if (listener != null) {
            listener.onChange(this);
        }
    }

    protected void fireOnInsert(Ptik ptik) {
        if (listener != null) {
            listener.onInsert(ptik);
        }
    }

    protected void fireOnUpdate(Ptik ptik) {
        if (listener != null) {
            listener.onUpdate(ptik);
        }
    }

    protected void fireOnDelete() {
        if (listener != null) {
            listener.onDelete();
        }
    }

    public void insertPtik() throws SQLException, PtikException {
        PtikDao dao = Database.getPtikDao();
        
        Ptik ptik = new Ptik();        
        ptik.setId(id);
        ptik.setNim(nim);
        ptik.setNama(nama);
        ptik.setTanggalLahir(tanggalLahir);
        ptik.setTelepon(telepon);
        ptik.setEmail(email);
        
        dao.insertPtik(ptik);
        
        fireOnInsert(ptik);
    }

    public void updatePtik() throws SQLException, PtikException {
        PtikDao dao = Database.getPtikDao();
        
        Ptik ptik = new Ptik();        
        ptik.setId(id);
        ptik.setNim(nim);
        ptik.setNama(nama);
        ptik.setTanggalLahir(tanggalLahir);
        ptik.setTelepon(telepon);
        ptik.setEmail(email);
        ptik.setId(id);
        
        dao.updatePtik(ptik);
        
        fireOnUpdate(ptik);
    }

    public void deletePtik() throws SQLException, PtikException {
        PtikDao dao = Database.getPtikDao();
        dao.deletePtik(id);
        fireOnDelete();
    }

    public void resetPtik() {
        setId(0);
        setNim("");
        setNama("");
        setTanggalLahir(new Date());
        setTelepon("");
        setEmail("");
    }
}
